package com.sodaopen.crm.common.controller;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import com.sodaopen.crm.common.entity.AjaxResult;
import com.sodaopen.crm.common.ex.CommonException;

@ControllerAdvice
@ResponseBody
public class ExceptionController {

	@ExceptionHandler(value = CommonException.class)
    public Object exceptionHandler(CommonException e){
		return new AjaxResult(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Object exceptionHandler(MethodArgumentNotValidException e){
        return AjaxResult.BAD_REQUEST("参数不正确", e.getBindingResult().getFieldError().getDefaultMessage());
    }

	@ExceptionHandler(value = Exception.class)
    public Object exceptionHandler(Exception e){
        e.printStackTrace();
	    if(e instanceof MethodArgumentTypeMismatchException){
            return AjaxResult.BAD_REQUEST("参数类型不匹配", e.getMessage());
        }

        return AjaxResult.ERROR("接口异常！");
    }
}
